package main

import (
	"flag"
	"fmt"
	"gitee.com/liumou_site/GoSpeed/model"
	"os"
)

var (
	urlFile   string
	Ver       bool
	Debug     bool
	ThreadNum int
	MaxSize   int64
	TimeStop  int
	TimeStart int
	MaxMemory int64
)

func main() {
	// 获取环境变量配置并记录日志
	defaultConfig := model.GetDefaultConfig()
	
	// 设置传参flag
	gfs := flag.NewFlagSet("GoSpeed", flag.ExitOnError)
	gfs.IntVar(&ThreadNum, "thread", defaultConfig.ThreadNum, "设置线程数")
	gfs.Int64Var(&MaxSize, "max", defaultConfig.MaxSize, "设置单文件最大下载大小(MB),当超出此值时,将自动跳过下载)")
	gfs.IntVar(&TimeStart, "start", defaultConfig.TimeStart, "设置开始时间(小时),默认: 9")
	gfs.IntVar(&TimeStop, "stop", defaultConfig.TimeStop, "设置停止时间(小时),默认: 23")
	gfs.StringVar(&urlFile, "urls", defaultConfig.Urls, "设置url文件")
	gfs.BoolVar(&Debug, "debug", defaultConfig.Debug, "显示调试信息")
	gfs.BoolVar(&Ver, "v", false, "显示版本号")
	gfs.Int64Var(&MaxMemory, "memory", defaultConfig.Memory, "设置最大内存限制(MB),默认: 2048")
	args := os.Args
	err := gfs.Parse(args[1:])
	if err != nil {
		return
	}
	if Ver {
		fmt.Println("1.0")
		os.Exit(0)
	}
	i := model.NewInfo()
	i.UrlFile = model.GetUrlFilePath(urlFile, Debug)
	i.TimeStop = TimeStop
	i.TimeStart = TimeStart
	i.Debug = Debug
	i.ThreadNum = ThreadNum
	// 设置最大内存限制
	if i.MemoryMon != nil {
		i.MemoryMon.SetMaxMemory(MaxMemory)
		if Debug {
			fmt.Printf("[DEBUG] 最大内存限制设置为: %d MB\n", MaxMemory)
		}
	}
	// 无限循环
	for {
		i.Start()
	}
}
